package goodroommate.ui.show;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import goodroommate.domain.UniversityDetail;
import goodroommate.exception.DatabaseException;
import goodroommate.exception.ItemNotExistsException;
import goodroommate.service.IUniversityService;
import goodroommate.service.impl.UniversityService;

/**
 * Servlet implementation class ShowUniversityByDomitoryServlet
 */
@WebServlet("/Show/ShowUniversityByDomitory")
public class ShowUniversityByDomitoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IUniversityService universityService=new UniversityService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowUniversityByDomitoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String struniversityid=request.getParameter("universityid");
		long universityid;
		try{
			universityid=Integer.parseInt(struniversityid);
		}catch(NumberFormatException e){
			universityid=0;
		}
		try{
			UniversityDetail universityDetail=universityService.getUniversityDetailById(universityid);
			request.setAttribute("universitydetail", universityDetail);
			request.getRequestDispatcher("showuniversitybydomitory.jsp").forward(request, response);
			return;
		}catch(ItemNotExistsException e){
			request.setAttribute("universitydetail", null);
			request.getRequestDispatcher("showuniversitybydomitory.jsp").forward(request, response);
			return;
		} catch (DatabaseException e) {
			response.sendError(500);
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
